// @flow
import axios from 'lib/apiClient';

export const sendWindowName = (windowName: string, provider: string): Promise<*> =>
    axios.post('/auth/send-window-name', { windowName, provider });
export const sendAuthEmail = (windowName: string, email: string): Promise<*> =>
    axios.post('/auth/send-auth-email', { windowName, email });
export const getCode = (code: string): Promise<*> => axios.get(`/auth/code/${code}`);
export const sendCode = (windowName: string, code: string): Promise<*> =>
    axios.post('/auth/send-code', { windowName, code });

type RegisterForm = {
    username: string,
    shortBio: string,
    displayName: string
};
export type LocalRegisterPayload = {
    registerToken: string,
    form: RegisterForm
};

export const localRegister = ({ registerToken, form }: LocalRegisterPayload): Promise<*> =>
    axios.post('/auth/register/local', {
        registerToken,
        form
    });

export const codeLogin = (code: string): Promise<*> => axios.post('/auth/code-login', { code });
export const check = (): Promise<*> => axios.get('/auth/check');
export const logout = (): Promise<*> => axios.post('/auth/logout');

export const verifySocial = (windowName: string, provider: string, redirectUri: string) =>
    axios.post(`/auth/verify-social/${provider}`, { windowName, redirectUri });

export type SocialRegisterPayload = {
    provider: string,
    accessToken: string,
    form: RegisterForm
};
export const socialRegister = ({ provider, accessToken, form }: SocialRegisterPayload) =>
    axios.post(`/auth/register/${provider}`, {
        accessToken,
        form
    });

export type SocialLoginPayload = { provider: string, accessToken: string };
export const socialLogin = ({ provider, accessToken }: SocialLoginPayload) =>
    axios.post(`/auth/login/${provider}`, { accessToken });
